package com.jiaai.util.spread_db_util;

import com.jiaai.gift.model.BGift;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * class_name:MemberHisUtil
 * describe:TODO
 * create_user:张昉平
 * create_date:2018/1/11
 * create_date:14:30
 */
public class DBHisUtil {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    private static final SimpleDateFormat yearMonth = new SimpleDateFormat("yyyy_MM");

    public static List<String> getYMBetween(Date std, Date edd){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar st = Calendar.getInstance();
        Calendar ed = Calendar.getInstance();
        st.setTime(std);
        ed.setTime(edd);

        int edMonth = ed.get(Calendar.MONTH);
        int stMonth = st.get(Calendar.MONTH);
        int edYear = ed.get(Calendar.YEAR);
        int stYear = st.get(Calendar.YEAR);
        int subMon = edMonth - stMonth;
        int subYear = (edYear - stYear) * 12;

        int sub = subYear + subMon;

        List<String> mms = new ArrayList<String>();

        for (int i = 0; i <= sub; i++) {
            ed.add(Calendar.MONTH, -i);
            mms.add(sdf.format(ed.getTime()));
            ed.add(Calendar.MONTH, i);
        }
        return mms;
    }

    public static Map getWalletInsertMap(String mId, BigDecimal money, String details, Integer type){
        Map map = new HashMap();
        map.put("tableName", getWalletHisTableName());
        map.put("mId", mId);
        map.put("money", money);
        map.put("details", details);
        map.put("type", type);
        map.put("createTime", new Date());
        return map;
    }

    public static Map getGiftMap(BGift gift, Integer glod){
        Map map = new HashMap();
        map.put("hisId", gift.getLiveId()+"_"+gift.getMemgerId()+"_"+gift.getGiftId());
        map.put("tableName", getGiftHisTableName());
        map.put("memberId", gift.getMemgerId());
        map.put("anchorId", gift.getAnchorId());
        map.put("giftNum", gift.getNum());
        map.put("giftId", gift.getGiftId());
        map.put("liveId", gift.getLiveId());
        map.put("price", glod);
        return map;
    }

    public static String getHbHisTableName(){
        return "t_hb_his_".concat(yearMonth.format(new Date()));
    }

    public static String getGiftHisTableName(){
        return "t_gift_his_".concat(sdf.format(new Date()));
    }

    public static String getGiftHisTableName(String year){
        return "t_gift_his_".concat(year);
    }

    public static String getWalletHisTableName(){
        return "t_member_wallet_history_".concat(sdf.format(new Date()));
    }

    public static String getHbHisTableName(String yearMonth){
        return "t_hb_his_".concat(yearMonth.replace("-", "_"));
    }
}
